#include "../Tools/GLHeaders.hpp"
#include "../Global.hpp"
#include "DisplayFunc.hpp"

#include <cmath>
#include "../Tools/Chrono.hpp"
#include "../Tools/Random.hpp"
#include "../Tools/ReferenceCube.hpp"
#include "../Tools/ReferenceFrame.hpp"
#include "../Tools/Matrix.hpp"
#include "../Tools/Vector.hpp"
#include <cstdio>

void DrawGLScene(void){
	static bool init = false;
	static Chrono time;
	static Random random;
	if(!init){
		time.Reset();
		time.Activate();
		random.SetSeed(8);
		init = true;
	}

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	glTranslatef(0.0f, 0.0f, -20.0f);
	//glRotatef(20 * time.GetTime(), 0.0f, 1.0f, 0.0f); // 360/20 = 18s for a turn over

	int lenght = 5;
	int a, b, c;
	int test = ((int)(time.GetTime()*1.0f)) % 2;
	static int val = 0;
	static unsigned int tmp = 0;
	if(val == test){
		tmp = (unsigned int)(random.Rand() * 1000);
		random.SetSeed(tmp);
		if(val == 0)
			val = 1;
		else
			val = 0;
	}
	else
		random.SetSeed(tmp);
	
	for(int i = -lenght; i <= lenght ; i++){
		for(int j = -lenght; j <= lenght; j++){
				a = (int)(random.Rand() * (lenght+1));
				b = (int)(random.Rand() * (lenght+1));
				c = (int)(random.Rand() * (lenght+1));
				if(i != a && j != b)
					continue;

				glPushMatrix();
				glTranslatef(1.5*i, 1.5*j , 0.0f);
				global_rot.glRotate();
				ReferenceFrame.Draw();
				ReferenceCube.Draw();
				glPopMatrix();
		}
	}

	glutSwapBuffers();
}
